<template>
    <view class="container">
        <oreder-header :placeholder="'输入采购单号/销售单号'" @searchFn="searchFn" @doFilter="doFilter"></oreder-header>
        <tab-bar :tabs="items" nobg="nobg" @clickTab="clickTab" :tabIndex="tabIndex"></tab-bar>
        <view class="main">
            <swiper
                class="swiper product-main-list"
                :style="{ height: listHeight }"
                :current="tabIndex"
                @change="swiperChange">
                <swiper-item v-for="(swiperItem, swiperIndex) in items" :key="swiperIndex">
                    <order-list
                        v-if="loadOk"
                        ref="productList"
                        :height="swiperItem.state == '2' ? sublistHeight : listHeight"
                        :state="swiperItem.state"
                        :requestData="requestData"></order-list>
                </swiper-item>
            </swiper>
        </view>
    </view>
</template>

<script>
import { mapGetters } from "vuex"
import orederHeader from "@/pages_supplier/components/order/order-header.vue"
import orderList from "@/pages_supplier/components/order/order-list.vue"
import tabBar from "@/pages_supplier/components/common/tab-bar.vue"
import storage from "@/uni-sub-base/utils/storage.js"
import { getElementHeight } from "@/uni-sub-base/utils/elementUtil.js"
export default {
    components: {
        orederHeader,
        orderList,
        tabBar,
    },
    data() {
        return {
            optEmpty: {
                icon: "/static/img/empty.png", // 图标路径 (建议放入static目录, 如 /static/img/mescroll-empty.png )
                tip: "~ 暂无数据 ~", // 空提示
            },
            isloadding: false,
            tabIndex: 0,
            items: [
                {
                    state: -1,
                    title: "全部",
                },
                {
                    state: 2,
                    title: "待接收",
                },
                {
                    state: 3,
                    title: "待发货",
                },
                {
                    state: 4,
                    title: "部分发货",
                },
                {
                    state: 5,
                    title: "已发货",
                },
                {
                    state: 6,
                    title: "已完成",
                },
                {
                    state: 7,
                    title: "已终止",
                },
            ],
            listHeight: "", //list所需高度
            sublistHeight: "", //list所需高度 待接收下面有按钮
            loadOk: false,
            searchKey: "",
            requestData: {
                state: -1,
                createType: -1,
                searchKey: "",
                startTime: "",
                endTime: "",
            },
        }
    },
    watch: {
        //监听更新
        getUpOrderView(e) {
            this.loadOk = false
            this.$nextTick(() => {
                this.loadOk = true
                this.refresh()
            })
        },
    },
    computed: {
        ...mapGetters(["getUpOrderView"]),
    },
    onLoad(e) {
        if (e.tabIndex) {
            this.tabIndex = e.tabIndex
        }
    },
    onBackPress(event) {
        this.easyReLaunch("/pages_supplier/packet/index/index")
        return true
    },
    onShow() {},
    mounted() {
        this.getListHeight()
    },
    methods: {
        getListHeight(element) {
            //获取商品高度
            getElementHeight(this, ".main", (e) => {
                this.listHeight = e + "px"
                this.sublistHeight = e - 60 + "px"
                this.loadOk = true
            })
        },
        swiperChange(e) {
            let tabIndex = e.detail.current
            this.clickTab(tabIndex)
        },
        //tab切换
        clickTab(index) {
            if (this.tabIndex != index) {
                this.tabIndex = index
            }
        },
        //搜索
        searchFn(searchKey) {
            this.requestData.searchKey = searchKey
            this.refresh()
        },
        //筛选
        doFilter(data) {
            this.requestData.startTime = data.startDate ? data.startDate + " 00:00:00" : ""
            this.requestData.endTime = data.endDate ? data.endDate + " 23:59:59" : ""
            this.refresh()
        },
        refresh() {
            this.$refs.productList.forEach((e) => {
                e.refresh(this.requestData)
            })
            // this.$refs.productList_0[0].refresh(this.requestData)
            // this.$refs.productList_1[0].refresh(this.requestData)
            // this.$refs.productList_2[0].refresh(this.requestData)
            // this.$refs.productList_3[0].refresh(this.requestData)
            // this.$refs.productList_4[0].refresh(this.requestData)
            // this.$refs.productList_5[0].refresh(this.requestData)
            // this.$refs.productList_6[0].refresh(this.requestData)
        },
    },
}
</script>

<style lang="scss" scoped>
.container {
    display: flex;
    flex-direction: column;
}
</style>
